package com.wzy.controller;

import com.wzy.pojo.User;
import com.wzy.service.UserService;
import com.wzy.utils.Page;
import com.wzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 根据id获取用户
     */
    @GetMapping("/get/{id}")
    public Result getUser(@PathVariable("id") String id) {
        User user = userService.getById(id);
        Result result = new Result(user);
        return result;
    }

    /**
     * 保存用户
     */
    @PostMapping("/save")
    public Result saveUser(@RequestBody User user) {
        userService.save(user);

        return new Result("保存成功");
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleted/{id}")
    public Result deletedById(@PathVariable("id") String id) {
        userService.deletedById(id);
        return new Result("删除成功");
    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        try {
            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("更新失败");
        }
        return new Result("更新成功");
    }

    /**
     * 分页查询用户
     */
    @PostMapping("/page")
    public Result page(@RequestBody Page<User> page) {
        page = userService.getByPage(page);
        return new Result(page);

    }
}
